<!DOCTYPE html>
<html>
    <?php
    include_once  "../DB/db.php";
    include_once  "../class/CategoryClass.php";
    include_once  "../class/ItemClass.php";
    include_once  "../Functions/imageFunctions.php";
    include_once  "../class/UserClass.php";


    if ($user->isLoggedOn() && $user->isAdmin()) {

        // Let value be undefined to block unneccecary warning messages:
        if(!isset($_GET['catId'])){
            $_GET['catId'] = "undefined";
        }

        // ------------------- New Item Display ----------------------
        function newItemDisplay(){
            global $cat;
            $catName = $cat->getCatName($_GET['catId']);?>

            <h1>Legg til ny vare i kategori: <?php echo $catName; ?></h1>
            <form id="addItemForm" name="newItem" method="POST" action="" enctype="multipart/form-data">
                <input type="hidden" name="kid" value="<?php echo $_GET['catId']; ?>"/>
                <label for="itemName">Name:</label><input id="itemName" type="text" name="itemName" required="required" maxlength="32"><br>
                <label for="itemPrice">Pris:</label><input id="itemPrice" type="text" name="itemPrice" required="required"/><br/>
                <label for="itemStock">Antall (ikkje nødvendig):</label><input id="itemStock" type="number" name="itemStock"/><br/>
                <label for="itemPicture">Bilde:</label><input type="file" id="itemPicture" name="itemPicture" required="required"/><br/>
                <label for="itemDescription">Beskrivelse:</label><br/>
                <textarea id="itemDescription" name="itemDescription" required="required"></textarea><br/>
                <input id="newItemSubmit" type="submit" name="newItemSubmit" value="Submit">
            </form>
     <?php }

     // Helps display categorys as select values:
     function dropDownCategory($kid){
        global $cat;
        $allCategorys = $cat->getAllLivingCategorys();
        while ($row = $allCategorys->fetch()) {
            $catName = $row['name'];
            $catId = $row['kid'];
             if($catId==$kid)
                echo "<option value='$catId' selected='selected'>$catName</option>";
             else
                echo "<option value='$catId' >$catName</option>";
        }
     }

     // -------------------- Alter Item ----------------------
      function alterItemDisplay(){
            global $cat;
            global $item;
            $itemData = $item->getAllItemData($_GET['itemId']); // get the neccecary values:
            $itemRow = $itemData->fetch();
            $catName = $cat->getCatName($itemRow['kid']);
            ?>
            
            <h1>Endre vare: <?php echo $itemRow['name'];?></h1>
            <form id="editItemForm" name="alterItem" method="POST" action="" enctype="multipart/form-data">
                <input type="hidden" name="vid" value="<?php echo $_GET['itemId'];?>"/>
                <label for="itemKid" >Kategori</label>
                <select name="categoryId">
                    <?php dropDownCategory($itemRow['kid']); ?>
                </select>
                <br/>
                <label for="itemName">Name:</label><input value="<?php echo $itemRow['name'];?>" id="itemName" type="text" name="itemName" required="required" maxlength="32"><br/>
                <label for="itemDeleted">Deleted:</label>
                <input type="radio" name="itemDeleted" value="yes" <?php if ($itemRow['deleted'] == 1) echo 'checked'; ?> />Yes<br />
                <input type="radio" name="itemDeleted" value="no"  <?php if ($itemRow['deleted'] == 0) echo 'checked'; ?> />No<br />
                <label for="itemPrice">Pris:</label><input value="<?php echo $itemRow['price'] ?>" type="text" name="itemPrice" required="required"/><br/>
                <label for="itemStock">Antall (ikkje nødvendig):</label><input value="<?php echo $itemRow['stock'];?>" id="itemStock" type="number" name="itemStock"/><br/>
                <label for="itemPicture">Bilde:</label><input type="file" id="itemPicture" name="itemPicture"/><br/>
                <label for="itemDescription">Beskrivelse:</label><br/>
                <textarea id="itemDescription" name="itemDescription" required="required">
                    <?php echo $itemRow['description'];?>
                </textarea><br/><br><br><br><br>
                <input id="alterItemSubmit" style="margin-left: 250px" type="submit" name="alterItemSubmit" value="Submit">
            </form>
     <?php }

     // ---------------------- Delete Item ----------------------
     function deleteItemDisplay(){
        global $item;
        $itemId = $_GET['itemId'];
        $itemName = $item->getItemName($itemId);
        ?>
        <h1>Slett vare</h1>
        <p>Er du sikkert på at du vil slette vare: <span id=delItemName><?php echo $itemName; ?></span>?</p>
        <form name="delItem" method="POST" action="">
            <input type='hidden' name='vid' value='<?php echo $itemId;?>'/>
            <input type="submit" id="delItemSubmit" name="delItemSubmit" value="Slett varen"/>
        </form>
        <p style="font-style: italic;">Merk: Å slette denne varen vil kun føre til at attributten "delete" <br>
            i databasen blir aktivert. Varen vil ikkje bli sletta før administrator <br>
            fjernar den frå databasa.</p>
     <?php }

    ?>
    <head>
        <title>Nettbutikk</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../CSS/mainFrame.css" />
        <link rel="stylesheet" type="text/css" href="../CSS/content.css" />
    </head>
    <body>
        <div class="wrapper">
            <?php
            include_once "../MainFrame/header.php";
            include_once "../MainFrame/menuBar.php";
            include_once "../MainFrame/sideBar.php";
            ?>

            <div class="content">
                <?php
                // Put proper data in global variables:
                if(isset($_POST['selectNewItemSubmit']) && $_POST['selectNewItemSubmit']) {
                    $_GET['alterItem'] = 'newItem';
                    $_GET['catId'] = $_POST['catId'];
                }

                // Redirect admin to the proper place:
                if(isset($_GET['alterItem']) && $_GET['alterItem'] == 'newItem') {
                    newItemDisplay();
                }
                else if(isset($_GET['alterItem']) && $_GET['alterItem'] == 'alterItem') {
                    alterItemDisplay();
                }
                else if(isset($_GET['alterItem']) && $_GET['alterItem'] == 'deleteItem') {
                    deleteItemDisplay();
                }
                ?>
            </div>

            <?php
            include_once "../MainFrame/footer.php";
            ?>
        </div>
    </body>


    <?php



// ************************* If new item ******************************
        if(isset($_POST['newItemSubmit']) && $_FILES['itemPicture']['size'] > 0 // if file is legal image file:
                && isImage($_FILES['itemPicture']['type']))
            {
            $fileName = $_FILES['itemPicture']['name'];     //Get image data:
            $tmpName  = $_FILES['itemPicture']['tmp_name'];
            $fileSize = $_FILES['itemPicture']['size'];
            $fileType = $_FILES['itemPicture']['type'];

            // Resize image.
            $content = $item->resizeImage($fileName, $tmpName, $fileSize, $fileType);

            // Database query.
            $item->insertItemDb($_POST['kid'], $_POST['itemName'], $_POST['itemPrice'],
                $_POST['itemDescription'], $content, $_POST['itemStock']);
            }
        else if(isset($_POST['newItemSubmit']) && isImage($_FILES['itemPicture']['type'])==false){ // if file is not legal image file:
            echo "Illegal file! This file is not an image.";
        }


 //********************** Alter item *************************
        if(isset($_POST['alterItemSubmit']) && $_POST['alterItemSubmit']){
            $itemId = $_POST['vid'];                        // Get posted data:
            $itemName = $_POST['itemName'];
            $itemDeleted = $_POST['itemDeleted'];
            $itemDescription = $_POST['itemDescription'];
            $itemPrice = $_POST['itemPrice'];
            $itemStock = $_POST['itemStock'];
            $catId = $_POST['categoryId'];
            $content = null;
            if ($itemDeleted == 'no') $itemDeleted = 0;
            else                     $itemDeleted = 1;

            //If the image is a legal image:
            if($_FILES['itemPicture']['size']>0 && isImage($_FILES['itemPicture']['type'])){
                $fileName = $_FILES['itemPicture']['name'];
                $tmpName  = $_FILES['itemPicture']['tmp_name'];
                $fileSize = $_FILES['itemPicture']['size'];
                $fileType = $_FILES['itemPicture']['type'];

                $content = $item->resizeImage($fileName, $tmpName, $fileSize, $fileType);
            }

            if($content!=null){ // If admin chose a new image file:
                $item->updateItemDbPicture($itemId, $catId, $itemName, $itemPrice, $itemDescription,
                    $content, $itemStock, $itemDeleted);
            }
            else{   // if admin didnt choose a new image file:
                $item->updateItemDb($itemId, $catId, $itemName, $itemPrice, $itemDescription,
                    $itemStock, $itemDeleted);
            }
        }

  // *********************** Delete Item **************************
        if (isset($_POST['delItemSubmit']) && $_POST['delItemSubmit']) {
            $item->deleteItem($_POST['vid']);
            header("Location: myAdminSite.php");
        }
    }
    ?>
</html>